package com.enqualcomm.kids.activities;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.enqualcomm.kids.BuildConfig;
import com.enqualcomm.kids.R;
import com.enqualcomm.kids.base.BaseActivity;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.network.NetworkListener;
import com.enqualcomm.kids.network.SocketRequest;
import com.enqualcomm.kids.network.socket.request.GetDownloadUrlParams;
import com.enqualcomm.kids.network.socket.response.GetDownloadUrlResult;
import com.google.zxing.WriterException;

import common.zxing.ZxingUtil;

public class ShareAppActivity extends BaseActivity implements View.OnClickListener {
    TextView app_name_tv;
    ImageView qrcode_iv;
    TextView desc_tv;
    private NetworkModel networkModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_share_app);
        networkModel = new NetworkModel();

        initView();
        initData();
    }

    private void initData() {
        networkModel.loadDataFromServer(new SocketRequest<GetDownloadUrlResult>(new GetDownloadUrlParams(BuildConfig.CHANNEL),
                new NetworkListener<GetDownloadUrlResult>() {
                    @Override
                    public void onError(VolleyError error) {
                        app_name_tv.setText(getString(R.string.in_progress));
                    }

                    @Override
                    public void onSuccess(GetDownloadUrlResult response) {
                        if (response.code == 0) {
                            if (TextUtils.isEmpty(response.result.url)) {
                                app_name_tv.setText(getString(R.string.in_progress));
                            } else {
                                app_name_tv.setText(getResources().getString(R.string.app_name));
                                desc_tv.setText(getResources().getString(R.string.scan_for_download));

                                try {
                                    qrcode_iv.setImageBitmap(ZxingUtil.create2DCode(response.result.url));
                                } catch (WriterException e) {
                                    e.printStackTrace();
                                }
                            }
                        } else {
                            app_name_tv.setText(getString(R.string.in_progress));
                        }
                    }
                }));
    }

    private void initView() {
        findViewById(R.id.title_bar_left_iv).setOnClickListener(this);
        TextView title_bar_name = (TextView) findViewById(R.id.title_bar_title_tv);
        title_bar_name.setText(getResources().getString(R.string.share_app_act_title));

        TextView title_bar_terminal_name_tv = (TextView) findViewById(R.id.title_bar_terminal_name_tv);
        View title_bar_terminal_icon_iv = findViewById(R.id.title_bar_terminal_icon_iv);
        title_bar_terminal_icon_iv.setVisibility(View.GONE);
        title_bar_terminal_name_tv.setVisibility(View.VISIBLE);
        title_bar_terminal_name_tv.setText(getString(R.string.share_app_act_title));

        app_name_tv = (TextView) findViewById(R.id.app_name_tv);
        desc_tv = (TextView) findViewById(R.id.desc_tv);
        qrcode_iv = (ImageView) findViewById(R.id.qrcode_iv);
//        ViewGroup.LayoutParams params = qrcode_iv.getLayoutParams();
//        params.width = params.height = (int) (GlobalParams.widowWidth*0.65f);
//        qrcode_iv.setLayoutParams(params);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    @Override
    protected void onStop() {
        super.onStop();
        networkModel.onStop();
    }
}
